package com.newenergy.wisehome.ui.adapter;

import android.content.Context;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.newenergy.wisehome.R;
import com.newenergy.wisehome.common.util.ImageUtil;
import com.newenergy.wisehome.data.bean.DeviceCategory;

import java.util.List;

/**
 * Created by jnl on 2017/9/14.
 */

public class MainGroupAdapter extends RecyclerArrayAdapter<DeviceCategory> {

    private ItemPowerCallBack mItemPowerCallBack;
    private Context mContext;

    //全局定义
    private long lastClickTime = 0L;
    private  int FAST_CLICK_DELAY_TIME = 1000;  // 快速点击间隔

    public void refreshData(List<DeviceCategory> data) {
        clear();
        if (data != null) addAll(data);
        notifyDataSetChanged();
    }

    public MainGroupAdapter(Context context) {
        super(context);
        mContext=context.getApplicationContext();
    }


    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {

        return new ViewHolder(parent);
    }


    private class ViewHolder extends BaseViewHolder<DeviceCategory> {
        private RelativeLayout ll_list_main;
        private ImageView ivIcon;
        private TextView tvName;
        private Button btnOn;
        private Button btnOff;
        private  Button btnOper;


        public ViewHolder(ViewGroup parent) {
            super(parent, R.layout.list_main);
            ll_list_main = $(R.id.ll_list_main);
            ivIcon = $(R.id.iv_icon);
            tvName = $(R.id.tv_name);
            btnOn = $(R.id.btn_on);
            btnOff = $(R.id.btn_off);
            btnOper=$(R.id.btn_oper);

            btnOn.setOnClickListener(v -> {
                if (mItemPowerCallBack != null)
                    mItemPowerCallBack.notifyPower(getDataPosition
                            (), "000100");
            });
            btnOff.setOnClickListener(v -> {
                if (mItemPowerCallBack != null)
                    mItemPowerCallBack.notifyPower(getDataPosition
                            (), "000000");
            });


            btnOper.setOnClickListener(v -> {

                if (mItemPowerCallBack!=null){
                    if (System.currentTimeMillis() - lastClickTime < FAST_CLICK_DELAY_TIME){
                        return;
                    }
                    lastClickTime = System.currentTimeMillis();
                    mItemPowerCallBack.notifyPage(getDataPosition());
                }

            });

        }

        @Override
        public void setData(DeviceCategory data) {
            super.setData(data);
            int imageId = ImageUtil.getImageId(data.getIcName());
            Glide.with(mContext).load(imageId).diskCacheStrategy(DiskCacheStrategy.NONE).into(ivIcon);

            if (data.getFamilyId().equals("12345678")&&!data.isChecked()) {
                ll_list_main.setBackgroundResource(R.drawable.bg_list_default_n);
            }else {
                ll_list_main.setBackgroundResource(R.drawable.bg_list_scene_selector);
            }

            tvName.setText(data.getName());
            ll_list_main.setSelected(data.isChecked());
            if (data.isChecked()){
                tvName.setTextColor(mContext.getResources().getColor(R.color.select_color));
            }else {
                tvName.setTextColor(mContext.getResources().getColor(R.color.content_color));
            }

        }
    }


    public void setItemPowerCallBack(ItemPowerCallBack itemPowerCallBack) {

        mItemPowerCallBack = itemPowerCallBack;
    }

    public interface ItemPowerCallBack {
        void notifyPower(int position, String cmd);

        void notifyPage(int position);
    }


}
